using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace CPI311.GameEngine
{
    public class ControledSprite : AnimatedSprite
    {
        public float Speed { get; protected set; }
        public float RotationSpeed { get; protected set; }
        public Vector2 TargetPossition { get; protected set; }
        public float TargetRotation { get; protected set; }

        public ControledSprite(Texture2D texture, int frameWidth, int frameHeight, int totalHeight, int totalWidth, int frames, List<Animation> animations, float speed, float rotationSpeed, Vector2 position): 
            base(texture, frameWidth, frameHeight, totalHeight, totalWidth, frames, animations, position)
        {
            Speed = speed;
            RotationSpeed = rotationSpeed;
            TargetPossition = position;
            TargetRotation = 0.0f;
        }

        public void setTarget(Vector2 target)
        {
            TargetPossition = target;
            TargetRotation = getRotation(target) % (float)(Math.PI *2);
        }

        public float getRotation(Vector2 target)
        {
            if(Position.X < target.X && Position.Y > target.Y)
            {
                //Quadrant I
                return (float)Math.Tan((target.X - Position.X) / (target.Y - Position.Y));
            }
            if(Position.X < target.X && Position.Y < target.Y) 
            {
                //Quadrant IV
                return (float)((Math.PI * 2) + Math.Tan((target.X - Position.X) / (target.Y - Position.Y)));
            }
            if(Position.X > target.X && Position.Y < target.Y)
            {
                //Quadrant III
                return (float)((Math.PI) + Math.Tan((target.X - Position.X) / (target.Y - Position.Y)));
            }
            if (Position.X > target.X && Position.Y > target.Y)
            {
                //Quadrant II
                return (float)((Math.PI) + Math.Tan((target.X - Position.X) / (target.Y - Position.Y)));
            }
            else
                return Rotation;
        }

        public void update()
        {
            if (Rotation == TargetRotation)
            {
                Position = TargetPossition;
            }
            if(Rotation + RotationSpeed < TargetRotation)
            {
                Rotation += RotationSpeed * Time.ElapsedGameTime;
            }
            if (Rotation - RotationSpeed > TargetRotation)
            {
                Rotation -= RotationSpeed * Time.ElapsedGameTime;
            }
            else
            {
                Rotation = TargetRotation;
            }
            Console.WriteLine(Rotation);
            base.update();
        }
    }
}